<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>获取当前资源使用状况</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.getopt.html">« getopt</a></li>
      <li style="float: right;"><a href="function.ini-alter.html">ini_alter »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.info.html">PHP 选项/信息 函数</a></li>
    <li>获取当前资源使用状况</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.getrusage" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">getrusage</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">getrusage</span> &mdash; <span class="dc-title">获取当前资源使用状况</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.getrusage-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>getrusage</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$mode</code><span class="initializer"> = 0</span></span>): <span class="type"><span class="type">array</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   这是 <strong class="command">getrusage(2)</strong> 的接口。它返回了调用自系统的数据。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.getrusage-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">mode</code></dt>

     <dd>

      <p class="para">
       如果 <code class="parameter">mode</code> 是 1，getrusage 会使用
       <strong><code>RUSAGE_CHILDREN</code></strong> 来调用。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.getrusage-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回了一个包含系统返回数据的关联数组。所以条目均可通过文档中字段的名称来访问。失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.getrusage-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.0.0</td>
       <td>
        此函数现在开始支持 Windows。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.getrusage-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-572">
    <p><strong>示例 #1 <span class="function"><strong>getrusage()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dat&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">getrusage</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #0000BB">$dat</span><span style="color: #007700">[</span><span style="color: #DD0000">"ru_oublock"</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;块输出操作数<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$dat</span><span style="color: #007700">[</span><span style="color: #DD0000">"ru_inblock"</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;块输入操作数<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$dat</span><span style="color: #007700">[</span><span style="color: #DD0000">"ru_msgsnd"</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;发送的&nbsp;IPC&nbsp;消息数<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$dat</span><span style="color: #007700">[</span><span style="color: #DD0000">"ru_msgrcv"</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;接收的&nbsp;IPC&nbsp;消息数<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$dat</span><span style="color: #007700">[</span><span style="color: #DD0000">"ru_maxrss"</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;maximum&nbsp;resident&nbsp;set&nbsp;size<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$dat</span><span style="color: #007700">[</span><span style="color: #DD0000">"ru_ixrss"</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;整数类型的共享内存大小<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$dat</span><span style="color: #007700">[</span><span style="color: #DD0000">"ru_idrss"</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;整数类型的非共享内存大小<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$dat</span><span style="color: #007700">[</span><span style="color: #DD0000">"ru_minflt"</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;页面回收次数（软分页错误）<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$dat</span><span style="color: #007700">[</span><span style="color: #DD0000">"ru_majflt"</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;页面错误次数（硬分页错误）<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$dat</span><span style="color: #007700">[</span><span style="color: #DD0000">"ru_nsignals"</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;接收到的信号数<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$dat</span><span style="color: #007700">[</span><span style="color: #DD0000">"ru_nvcsw"</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;number&nbsp;of&nbsp;voluntary&nbsp;context&nbsp;switches<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$dat</span><span style="color: #007700">[</span><span style="color: #DD0000">"ru_nivcsw"</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;number&nbsp;of&nbsp;involuntary&nbsp;context&nbsp;switches<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$dat</span><span style="color: #007700">[</span><span style="color: #DD0000">"ru_nswap"</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;交换次数<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$dat</span><span style="color: #007700">[</span><span style="color: #DD0000">"ru_utime.tv_usec"</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;用户使用时间（微秒）<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$dat</span><span style="color: #007700">[</span><span style="color: #DD0000">"ru_utime.tv_sec"</span><span style="color: #007700">];&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;用户使用时间（秒）<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$dat</span><span style="color: #007700">[</span><span style="color: #DD0000">"ru_stime.tv_usec"</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;系统使用时间（微秒）<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 notes" id="refsect1-function.getrusage-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    在 Windows 上 <span class="function"><strong>getrusage()</strong></span> 仅会返回以下类型：
   </p>
   <p class="para">
    <ul class="simplelist">
     <li class="member"><code class="literal">&quot;ru_stime.tv_sec&quot;</code></li>
     <li class="member"><code class="literal">&quot;ru_stime.tv_usec&quot;</code></li>
     <li class="member"><code class="literal">&quot;ru_utime.tv_sec&quot;</code></li>
     <li class="member"><code class="literal">&quot;ru_utime.tv_usec&quot;</code></li>
     <li class="member">
      <code class="literal">&quot;ru_majflt&quot;</code>（仅当 <code class="parameter">mode</code> 是
      <strong><code>RUSAGE_SELF</code></strong>）
     </li>
     <li class="member">
      <code class="literal">&quot;ru_maxrss&quot;</code>（仅当 <code class="parameter">mode</code> 是
      <strong><code>RUSAGE_SELF</code></strong>）
     </li>
    </ul>
   </p>
   <p class="para">
    如果使用设置 <code class="parameter">mode</code> 为 
    <code class="literal">1</code>（<strong><code>RUSAGE_CHILDREN</code></strong>）的情况下调用
    <span class="function"><strong>getrusage()</strong></span>，则会收集线程的资源使用情况（意味着在内部使用
    <strong><code>RUSAGE_THREAD</code></strong> 调用此函数）。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    在 BeOS 2000，仅会返回以下类型：
   </p>
   <p class="para">
    <ul class="simplelist">
     <li class="member"><code class="literal">&quot;ru_stime.tv_sec&quot;</code></li>
     <li class="member"><code class="literal">&quot;ru_stime.tv_usec&quot;</code></li>
     <li class="member"><code class="literal">&quot;ru_utime.tv_sec&quot;</code></li>
     <li class="member"><code class="literal">&quot;ru_utime.tv_usec&quot;</code></li>
    </ul>
   </p>
  </p></blockquote>
 </div>



 <div class="refsect1 seealso" id="refsect1-function.getrusage-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member">系统上 <strong class="command">getrusage(2)</strong> 的 man 页面</li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>